﻿/**  版本信息模板在安装目录下，可自行修改。
* Product.cs
*
* 功 能： N/A
* 类 名： Product
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2013-4-17 09:50:42   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:Product
    /// </summary>
    public partial class Product
    {
        public Product()
        {
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from Product");
            strSql.Append(" where ID=@ID");
            var parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.BigInt)
            };
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(DMZS.Model.Product model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into Product(");
            strSql.Append("TypeID,Code,UserCode,Name,FullName,NamePY,Specification,Model,IsStop,UnitID,UnitID1,ChangeRate,Remark)");
            strSql.Append(" values (");
            strSql.Append("@TypeID,@Code,@UserCode,@Name,@FullName,@NamePY,@Specification,@Model,@IsStop,@UnitID,@UnitID1,@ChangeRate,@Remark)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@TypeID", SqlDbType.BigInt, 8),
                    new SqlParameter("@Code", SqlDbType.NVarChar, 50),
                    new SqlParameter("@UserCode", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Name", SqlDbType.NVarChar, 100),
                    new SqlParameter("@FullName", SqlDbType.NVarChar, 100),
                    new SqlParameter("@NamePY", SqlDbType.NVarChar, 100),
                    new SqlParameter("@Specification", SqlDbType.NVarChar, 200),
                    new SqlParameter("@Model", SqlDbType.NVarChar, 100),
                    new SqlParameter("@IsStop", SqlDbType.Bit, 1),
                    new SqlParameter("@UnitID", SqlDbType.Int, 4),
                    new SqlParameter("@UnitID1", SqlDbType.Int, 4),
                    new SqlParameter("@ChangeRate", SqlDbType.Decimal, 9),
                    new SqlParameter("@Remark", SqlDbType.NVarChar, 200) };
            parameters[0].Value = model.TypeID;
            parameters[1].Value = model.Code;
            parameters[2].Value = model.UserCode;
            parameters[3].Value = model.Name;
            parameters[4].Value = model.FullName;
            parameters[5].Value = model.NamePY;
            parameters[6].Value = model.Specification;
            parameters[7].Value = model.Model;
            parameters[8].Value = model.IsStop;
            parameters[9].Value = model.UnitID;
            parameters[10].Value = model.UnitID1;
            parameters[11].Value = model.ChangeRate;
            parameters[12].Value = model.Remark;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.Product model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update Product set ");
            strSql.Append("TypeID=@TypeID,");
            strSql.Append("Code=@Code,");
            strSql.Append("UserCode=@UserCode,");
            strSql.Append("Name=@Name,");
            strSql.Append("FullName=@FullName,");
            strSql.Append("NamePY=@NamePY,");
            strSql.Append("Specification=@Specification,");
            strSql.Append("Model=@Model,");
            strSql.Append("IsStop=@IsStop,");
            strSql.Append("UnitID=@UnitID,");
            strSql.Append("UnitID1=@UnitID1,");
            strSql.Append("ChangeRate=@ChangeRate,");
            strSql.Append("Remark=@Remark");
            strSql.Append(" where ID=@ID");
            var parameters = new SqlParameter[] { new SqlParameter("@TypeID", SqlDbType.BigInt, 8),
                    new SqlParameter("@Code", SqlDbType.NVarChar, 50),
                    new SqlParameter("@UserCode", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Name", SqlDbType.NVarChar, 100),
                    new SqlParameter("@FullName", SqlDbType.NVarChar, 100),
                    new SqlParameter("@NamePY", SqlDbType.NVarChar, 100),
                    new SqlParameter("@Specification", SqlDbType.NVarChar, 200),
                    new SqlParameter("@Model", SqlDbType.NVarChar, 100),
                    new SqlParameter("@IsStop", SqlDbType.Bit, 1),
                    new SqlParameter("@UnitID", SqlDbType.Int, 4),
                    new SqlParameter("@UnitID1", SqlDbType.Int, 4),
                    new SqlParameter("@ChangeRate", SqlDbType.Decimal, 9),
                    new SqlParameter("@Remark", SqlDbType.NVarChar, 200),
                    new SqlParameter("@ID", SqlDbType.BigInt, 8) };
            parameters[0].Value = model.TypeID;
            parameters[1].Value = model.Code;
            parameters[2].Value = model.UserCode;
            parameters[3].Value = model.Name;
            parameters[4].Value = model.FullName;
            parameters[5].Value = model.NamePY;
            parameters[6].Value = model.Specification;
            parameters[7].Value = model.Model;
            parameters[8].Value = model.IsStop;
            parameters[9].Value = model.UnitID;
            parameters[10].Value = model.UnitID1;
            parameters[11].Value = model.ChangeRate;
            parameters[12].Value = model.Remark;
            parameters[13].Value = model.ID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from Product ");
            strSql.Append(" where ID=@ID");
            var parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.BigInt)
            };
            parameters[0].Value = ID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from Product ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.Product GetModel(long ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,TypeID,Code,UserCode,Name,FullName,NamePY,Specification,Model,IsStop,UnitID,UnitID1,ChangeRate,Remark from Product ");
            strSql.Append(" where ID=@ID");
            var parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.BigInt)
            };
            parameters[0].Value = ID;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.Product DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.Product();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != string.Empty)
                {
                    model.ID = long.Parse(row["ID"].ToString());
                }
                if (row["TypeID"] != null && row["TypeID"].ToString() != string.Empty)
                {
                    model.TypeID = long.Parse(row["TypeID"].ToString());
                }
                if (row["Code"] != null)
                {
                    model.Code = row["Code"].ToString();
                }
                if (row["UserCode"] != null)
                {
                    model.UserCode = row["UserCode"].ToString();
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["FullName"] != null)
                {
                    model.FullName = row["FullName"].ToString();
                }
                if (row["NamePY"] != null)
                {
                    model.NamePY = row["NamePY"].ToString();
                }
                if (row["Specification"] != null)
                {
                    model.Specification = row["Specification"].ToString();
                }
                if (row["Model"] != null)
                {
                    model.Model = row["Model"].ToString();
                }
                if (row["IsStop"] != null && row["IsStop"].ToString() != string.Empty)
                {
                    if ((row["IsStop"].ToString() == "1") || (row["IsStop"].ToString().ToLower() == "true"))
                    {
                        model.IsStop = true;
                    }
                    else
                    {
                        model.IsStop = false;
                    }
                }
                if (row["UnitID"] != null && row["UnitID"].ToString() != string.Empty)
                {
                    model.UnitID = int.Parse(row["UnitID"].ToString());
                }
                if (row["UnitID1"] != null && row["UnitID1"].ToString() != string.Empty)
                {
                    model.UnitID1 = int.Parse(row["UnitID1"].ToString());
                }
                if (row["ChangeRate"] != null && row["ChangeRate"].ToString() != string.Empty)
                {
                    model.ChangeRate = decimal.Parse(row["ChangeRate"].ToString());
                }
                if (row["Remark"] != null)
                {
                    model.Remark = row["Remark"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ID,TypeID,Code,UserCode,Name,FullName,NamePY,Specification,Model,IsStop,UnitID,UnitID1,ChangeRate,Remark ");
            strSql.Append(" FROM Product ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,TypeID,Code,UserCode,Name,FullName,NamePY,Specification,Model,IsStop,UnitID,UnitID1,ChangeRate,Remark ");
            strSql.Append(" FROM Product ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Product ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from Product T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表 扩展
        /// </summary>
        /// <param name="ProductTypeID">产品类型ID, 0:全部</param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetListForUIExpand(long ProductTypeID, string strWhere)
        {
            var strSql = @"
if @ProductTypeID<>0 begin
	select p.ID,p.TypeID,p.Code,p.UserCode,p.Name,p.FullName,p.NamePY,p.Specification,p.Model,p.IsStop,p.UnitID,p.UnitID1,p.ChangeRate, 
		pt.Code ProductTypeNo,pt.Name ProductTypeName,
		u.BaseCode,u.BaseName,
		u1.BaseCode BaseCode1,u1.BaseName BaseName1
	from Product p
	join ProductType pt on pt.ID=p.TypeID
	left join UnitBase u on u.UnitBaseID=p.UnitID
	left join UnitBase u1 on u1.UnitBaseID=p.UnitID1
	where pt.ID=@ProductTypeID and {0:%s}
end else begin
	select p.ID,p.TypeID,p.Code,p.UserCode,p.Name,p.FullName,p.NamePY,p.Specification,p.Model,p.IsStop,p.UnitID,p.UnitID1,p.ChangeRate, 
		pt.Code ProductTypeNo,pt.Name ProductTypeName,
		u.BaseCode,u.BaseName,
		u1.BaseCode BaseCode1,u1.BaseName BaseName1
	from Product p
	join ProductType pt on pt.ID=p.TypeID
	left join UnitBase u on u.UnitBaseID=p.UnitID
	left join UnitBase u1 on u1.UnitBaseID=p.UnitID1
    where {0:%s} 
end";

            strSql = string.Format(strSql, strWhere.Trim().Length == 0 ? "1=1" : strWhere);
            var parameters = new SqlParameter[] { new SqlParameter("@ProductTypeID", SqlDbType.BigInt, 8)
            };
            parameters[0].Value = ProductTypeID;

            return DbHelperSQL.Query(strSql, parameters);
        }
        /// <summary>
        /// 获取数据行
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public DataRow GetRecordByID(long ID)
        {
            var strSql = @"
	select p.ID,p.TypeID,p.Code,p.UserCode,p.Name,p.FullName,p.NamePY,p.Specification,p.Model,p.IsStop,p.UnitID,p.UnitID1,p.ChangeRate,p.Remark, 
		pt.Code ProductTypeNo,pt.Name ProductTypeName,
		u.BaseCode,u.BaseName,
		u1.BaseCode BaseCode1,u1.BaseName BaseName1
	from Product p
	join ProductType pt on pt.ID=p.TypeID
	left join UnitBase u on u.UnitBaseID=p.UnitID
	left join UnitBase u1 on u1.UnitBaseID=p.UnitID1
    where p.ID=" + ID.ToString();

            var ds = DbHelperSQL.Query(strSql);
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            else
            {
                return ds.Tables[0].Rows[0];
            }
        }

        /// <summary>
        /// 增加一条数据 扩展
        /// </summary>
        public long AddExpand(DMZS.Model.Product model)
        {
            var strSql = @"
declare @typeCount int, @codeCount int, @unitCount int, @unitCount1 int
select @codeCount=count(1) from Product where Code=@Code
if @codeCount>0 begin
    select -1000    --编号重复
    return
end

select @typeCount=count(1) from ProductType where ID=@TypeID
if @typeCount=0 begin
    select -1001    --产品类型不存在
    return
end

if @UnitID<>0 begin
    select @unitCount=count(1) from UnitBase where UnitBaseID=@UnitID
    if @unitCount=0 begin
        select -1002    --单位不存在
        return
    end
end

if @UnitID1<>0 begin
    select @unitCount1=count(1) from UnitBase where UnitBaseID=@UnitID1
    if @unitCount1=0 begin
        select -1003    --辅助单位不存在
        return
    end
end

insert into Product(TypeID,Code,UserCode,Name,FullName,NamePY,Specification,Model,IsStop,UnitID,UnitID1,ChangeRate,Remark)
values (@TypeID,@Code,@UserCode,@Name,@FullName,@NamePY,@Specification,@Model,@IsStop,@UnitID,@UnitID1,@ChangeRate,@Remark)
select @@IDENTITY";

            var parameters = new SqlParameter[] { new SqlParameter("@TypeID", SqlDbType.BigInt, 8),
                    new SqlParameter("@Code", SqlDbType.NVarChar, 50),
                    new SqlParameter("@UserCode", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Name", SqlDbType.NVarChar, 100),
                    new SqlParameter("@FullName", SqlDbType.NVarChar, 100),
                    new SqlParameter("@NamePY", SqlDbType.NVarChar, 100),
                    new SqlParameter("@Specification", SqlDbType.NVarChar, 200),
                    new SqlParameter("@Model", SqlDbType.NVarChar, 100),
                    new SqlParameter("@IsStop", SqlDbType.Bit, 1),
                    new SqlParameter("@UnitID", SqlDbType.Int, 4),
                    new SqlParameter("@UnitID1", SqlDbType.Int, 4),
                    new SqlParameter("@ChangeRate", SqlDbType.Decimal, 9),
                    new SqlParameter("@Remark", SqlDbType.NVarChar, 200) };
            parameters[0].Value = model.TypeID;
            parameters[1].Value = model.Code;
            parameters[2].Value = model.UserCode;
            parameters[3].Value = model.Name;
            parameters[4].Value = model.FullName;
            parameters[5].Value = model.NamePY;
            parameters[6].Value = model.Specification;
            parameters[7].Value = model.Model;
            parameters[8].Value = model.IsStop;
            parameters[9].Value = model.UnitID;
            parameters[10].Value = model.UnitID1;
            parameters[11].Value = model.ChangeRate;
            parameters[12].Value = model.Remark;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public long UpdateExpand(DMZS.Model.Product model)
        {
            var strSql = @"
declare @typeCount int, @codeCount int, @unitCount int, @unitCount1 int
select @codeCount=count(1) from Product where Code=@Code and ID<>@ID
if @codeCount>0 begin
    select -1000    --编号重复
    return
end

select @typeCount=count(1) from ProductType where ID=@TypeID
if @typeCount=0 begin
    select -1001    --产品类型不存在
    return
end

if @UnitID<>0 begin
    select @unitCount=count(1) from UnitBase where UnitBaseID=@UnitID
    if @unitCount=0 begin
        select -1002    --单位不存在
        return
    end
end

if @UnitID1<>0 begin
    select @unitCount1=count(1) from UnitBase where UnitBaseID=@UnitID1
    if @unitCount1=0 begin
        select -1003    --辅助单位不存在
        return
    end
end

update Product set 
    TypeID=@TypeID,
    Code=@Code,
    UserCode=@UserCode,
    Name=@Name,
    FullName=@FullName,
    NamePY=@NamePY,
    Specification=@Specification,
    Model=@Model,
    IsStop=@IsStop,
    UnitID=@UnitID,
    UnitID1=@UnitID1,
    ChangeRate=@ChangeRate,
    Remark=@Remark
where ID=@ID
select @ID
";
            var parameters = new SqlParameter[] { new SqlParameter("@TypeID", SqlDbType.BigInt, 8),
                    new SqlParameter("@Code", SqlDbType.NVarChar, 50),
                    new SqlParameter("@UserCode", SqlDbType.NVarChar, 50),
                    new SqlParameter("@Name", SqlDbType.NVarChar, 100),
                    new SqlParameter("@FullName", SqlDbType.NVarChar, 100),
                    new SqlParameter("@NamePY", SqlDbType.NVarChar, 100),
                    new SqlParameter("@Specification", SqlDbType.NVarChar, 200),
                    new SqlParameter("@Model", SqlDbType.NVarChar, 100),
                    new SqlParameter("@IsStop", SqlDbType.Bit, 1),
                    new SqlParameter("@UnitID", SqlDbType.Int, 4),
                    new SqlParameter("@UnitID1", SqlDbType.Int, 4),
                    new SqlParameter("@ChangeRate", SqlDbType.Decimal, 9),
                    new SqlParameter("@Remark", SqlDbType.NVarChar, 200),
                    new SqlParameter("@ID", SqlDbType.BigInt, 8) };
            parameters[0].Value = model.TypeID;
            parameters[1].Value = model.Code;
            parameters[2].Value = model.UserCode;
            parameters[3].Value = model.Name;
            parameters[4].Value = model.FullName;
            parameters[5].Value = model.NamePY;
            parameters[6].Value = model.Specification;
            parameters[7].Value = model.Model;
            parameters[8].Value = model.IsStop;
            parameters[9].Value = model.UnitID;
            parameters[10].Value = model.UnitID1;
            parameters[11].Value = model.ChangeRate;
            parameters[12].Value = model.Remark;
            parameters[13].Value = model.ID;

            var result = DbHelperSQL.GetSingle(strSql, parameters);
            if (result != null)
            {
                return Convert.ToInt64(result);
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 第一条记录
        /// </summary>
        /// <returns></returns>
        public DataRow GetFirst()
        {
            var strSql = @"
	select top 1 p.ID,p.TypeID,p.Code,p.UserCode,p.Name,p.FullName,p.NamePY,p.Specification,p.Model,p.IsStop,p.UnitID,p.UnitID1,p.ChangeRate,p.Remark, 
		pt.Code ProductTypeNo,pt.Name ProductTypeName,
		u.BaseCode,u.BaseName,
		u1.BaseCode BaseCode1,u1.BaseName BaseName1
	from Product p
	join ProductType pt on pt.ID=p.TypeID
	left join UnitBase u on u.UnitBaseID=p.UnitID
	left join UnitBase u1 on u1.UnitBaseID=p.UnitID1";

            var ds = DbHelperSQL.Query(strSql);
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            else
            {
                return ds.Tables[0].Rows[0];
            }
        }

        /// <summary>
        /// 最后一条记录
        /// </summary>
        /// <returns></returns>
        public DataRow GetLast()
        {
            var strSql = @"
	select top 1 p.ID,p.TypeID,p.Code,p.UserCode,p.Name,p.FullName,p.NamePY,p.Specification,p.Model,p.IsStop,p.UnitID,p.UnitID1,p.ChangeRate,p.Remark, 
		pt.Code ProductTypeNo,pt.Name ProductTypeName,
		u.BaseCode,u.BaseName,
		u1.BaseCode BaseCode1,u1.BaseName BaseName1
	from Product p
	join ProductType pt on pt.ID=p.TypeID
	left join UnitBase u on u.UnitBaseID=p.UnitID
	left join UnitBase u1 on u1.UnitBaseID=p.UnitID1
    order by p.ID desc";

            var ds = DbHelperSQL.Query(strSql.ToString());
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            return ds.Tables[0].Rows[0];
        }

        /// <summary>
        /// 上一条记录
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public DataRow GetPrevious(long ID)
        {
            var strSql = @"
	select top 1 p.ID,p.TypeID,p.Code,p.UserCode,p.Name,p.FullName,p.NamePY,p.Specification,p.Model,p.IsStop,p.UnitID,p.UnitID1,p.ChangeRate,p.Remark, 
		pt.Code ProductTypeNo,pt.Name ProductTypeName,
		u.BaseCode,u.BaseName,
		u1.BaseCode BaseCode1,u1.BaseName BaseName1
	from Product p
	join ProductType pt on pt.ID=p.TypeID
	left join UnitBase u on u.UnitBaseID=p.UnitID
	left join UnitBase u1 on u1.UnitBaseID=p.UnitID1
    where p.ID<@ID
    order by p.ID desc";
            var parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.BigInt)
            };
            parameters[0].Value = ID;

            var ds = DbHelperSQL.Query(strSql, parameters);
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            return ds.Tables[0].Rows[0];
        }

        /// <summary>
        /// 下一条记录
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public DataRow GetNext(long ID)
        {
            var strSql = @"
	select top 1 p.ID,p.TypeID,p.Code,p.UserCode,p.Name,p.FullName,p.NamePY,p.Specification,p.Model,p.IsStop,p.UnitID,p.UnitID1,p.ChangeRate,p.Remark, 
		pt.Code ProductTypeNo,pt.Name ProductTypeName,
		u.BaseCode,u.BaseName,
		u1.BaseCode BaseCode1,u1.BaseName BaseName1
	from Product p
	join ProductType pt on pt.ID=p.TypeID
	left join UnitBase u on u.UnitBaseID=p.UnitID
	left join UnitBase u1 on u1.UnitBaseID=p.UnitID1
    where p.ID>@ID
    order by p.ID";
            var parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.BigInt)
            };
            parameters[0].Value = ID;

            var ds = DbHelperSQL.Query(strSql, parameters);
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            return ds.Tables[0].Rows[0];
        }




        /// <summary>
        /// 获取行记录
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public DataRow GetRecordByCode(string Code)
        {
            var strSql = @"
	select p.ID,p.TypeID,p.Code,p.UserCode,p.Name,p.FullName,p.NamePY,p.Specification,p.Model,p.IsStop,p.UnitID,p.UnitID1,p.ChangeRate,p.Remark, 
		pt.Code ProductTypeNo,pt.Name ProductTypeName,
		u.BaseCode,u.BaseName,
		u1.BaseCode BaseCode1,u1.BaseName BaseName1
	from Product p
	join ProductType pt on pt.ID=p.TypeID
	left join UnitBase u on u.UnitBaseID=p.UnitID
	left join UnitBase u1 on u1.UnitBaseID=p.UnitID1
    where p.Code=@Code";

            var parameters = new SqlParameter[] { new SqlParameter("@Code", SqlDbType.NVarChar, 50)
            };
            parameters[0].Value = Code;

            var ds = DbHelperSQL.Query(strSql, parameters);
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            else
            {
                return ds.Tables[0].Rows[0];
            }
        }

        public DataSet GetListForDialog(string ProductCode, string ProductName, string Model, string Specification)
        {
            var strSql = @"
	select p.ID,p.TypeID,p.Code,p.UserCode,p.Name,p.FullName,p.NamePY,p.Specification,p.Model,p.IsStop,p.UnitID,p.UnitID1,p.ChangeRate,p.Remark, 
		pt.Code ProductTypeNo,pt.Name ProductTypeName,
		u.BaseCode,u.BaseName,
		u1.BaseCode BaseCode1,u1.BaseName BaseName1
	from Product p
	join ProductType pt on pt.ID=p.TypeID
	left join UnitBase u on u.UnitBaseID=p.UnitID
	left join UnitBase u1 on u1.UnitBaseID=p.UnitID1
    where p.Code like '%'+@Code+'%' and p.Name like '%'+@Name+'%' and  p.Model like '%'+@Model+'%' and p.Specification like '%'+@Specification+'%' ";

            var parameters = new SqlParameter[] { 
                new SqlParameter("@Code", SqlDbType.NVarChar, 50),
                new SqlParameter("@Name", SqlDbType.NVarChar, 100),
                new SqlParameter("@Model", SqlDbType.NVarChar, 100),
                new SqlParameter("@Specification", SqlDbType.NVarChar, 200)
            };
            parameters[0].Value = ProductCode;
            parameters[1].Value = ProductName;
            parameters[2].Value = Model;
            parameters[3].Value = Specification;

            return DbHelperSQL.Query(strSql, parameters);
        }

        /// <summary>
        /// 删除一条记录
        /// false 有引用项，无法删除
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool DeleteExpand(long ID)
        {
            var strSql = "pDeleteProduct";

            var parameters = new SqlParameter[] { 
                new SqlParameter("@ProductID", SqlDbType.BigInt, 8),
                new SqlParameter("@Result", SqlDbType.Bit, 1)
            };
            parameters[0].Value = ID;
            parameters[1].Direction = ParameterDirection.Output;

            DbHelperSQL.RunProcedure(strSql, parameters);

            return Convert.ToBoolean(parameters[1].Value);
        }

        public DMZS.Model.Product GetModel(string Code)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,TypeID,Code,UserCode,Name,FullName,NamePY,Specification,Model,IsStop,UnitID,UnitID1,ChangeRate,Remark from Product ");
            strSql.Append(" where Code=@Code");
            var parameters = new SqlParameter[] { new SqlParameter("@Code", SqlDbType.NVarChar, 50)
            };
            parameters[0].Value = Code;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
    }
}

